package com.sinoglobal.ningxia.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_UserInfoVo;
import com.sinoglobal.ningxia.beans.LoginVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.thread.ItktAsyncTask;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.utils.UserUtils;
import com.sinoglobal.shakeprize.utils.Contstent;
import com.sinoglobal.shakeprize.utils.SPUtil;
import com.sinoglobal.sinostore.SinoStore;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

public class LoginReceiver extends BroadcastReceiver {
    public static final String ACTION = Dumpling_SinoConstans.DUMPLING_LOGIN_ACTION;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("登录请求广播", "请求广播" + intent.getAction());
        if (ACTION.equals(intent.getAction())) {
            Dumpling_UserInfoVo userInfoVo = (Dumpling_UserInfoVo) intent.getSerializableExtra("userInfo");
            login(context, userInfoVo);
            Log.i("登录请求广播", "请求信息===" + userInfoVo.toString());
        }

    }

    private void login(final Context context, final Dumpling_UserInfoVo userInfoVo) {
        new ItktAsyncTask<Void, Void, LoginVo>() {

            @Override
            public LoginVo before(Void... params) throws Exception {
                return RemoteImpl.getInstance().dumplingLogin(userInfoVo.getId(), userInfoVo.getUserName(), "2", userInfoVo.getNickname(), "2");
            }

            @Override
            public void after(final LoginVo result) {
                if (result == null || Code.CODE_SUCCESS != result.getCode()) {
                    return;
                }
                new Thread() {
                    public void run() {
                        HttpGet httpRequest = new HttpGet(ConnectionUtil.PORTAL_VAILED + result.getUsername());
                        try {
                            // 发送请求并等待响应
                            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);

                            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                                String strResult;
                                strResult = EntityUtils.toString(httpResponse.getEntity());
                                JSONObject jb = new JSONObject(strResult);
                                // 1代表验证通过可以显示会议入口
                                if (jb.getString("code").equals("1")) {
                                    SharedPreferenceUtil.saveBoolean(context, SharedPreferenceUtil.IS_PORTAL_FLAG, true);
                                } else {
                                    SharedPreferenceUtil.saveBoolean(context, SharedPreferenceUtil.IS_PORTAL_FLAG, false);
                                }
                                //live 为1 标志这个人可直播
                                if (jb.getString("live").equals("1")) {
                                    SharedPreferenceUtil.saveBoolean(context, SharedPreferenceUtil.IS_LIVE_FLAG, true);
                                    //保存推流地址 2017年5月9日09:27:46
                                    SharedPreferenceUtil.saveString(context, SharedPreferenceUtil.LIVE_LOCATION, jb.getString("rtmp"));
                                } else {
                                    SharedPreferenceUtil.saveBoolean(context, SharedPreferenceUtil.IS_LIVE_FLAG, false);
                                }

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                FlyApplication.userId = result.getId();
                SPUtil.saveString(context, Contstent.USER_ID, result.getId());
                setShareData(context, result);

            }

            @Override
            public void exception() {

            }

        }.execute();
    }

    // 设置share存储登录信息
    private void setShareData(Context context, LoginVo result) {
        // 初始化商城
        SinoStore.initData(result.getId(), result.getUserCenterId(), result.getUsername(), result.getNickname(), result.getScore());
        UserUtils.LoginSuccess(result);
        Intent jifen = new Intent(MContants.BROADCAST_JIFEN);
        context.sendBroadcast(jifen);
    }

}
